package com.xrtn.controller;

import com.xrtn.bean.SysUserInfo;
import com.xrtn.service.ICommonService;
import com.xrtn.service.UploadFileService;
import com.xrtn.utils.CodeEnum;
import com.xrtn.utils.HttpUtils;
import com.xrtn.utils.ObjUtils;
import com.xrtn.utils.ResponseData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by tu_pei on 2018/4/9.
 */
@Api
@Controller
@RequestMapping("/excel")
public class ExcelController {

    @Autowired
    private UploadFileService uploadFileService;
    @Autowired
    private ICommonService commonService;

    /**
     * @Title: saveUserCard
     * @Description: 导出用户预约信息
     * @param @return
     * @return ResponseData
     * @throws
     */
    @ApiOperation(value = "导出用户预约信息",notes="导出用户预约信息")
    @RequestMapping(value = "/exportBookingList",method = RequestMethod.GET)
    @ResponseBody
    public ResponseData exportBookingList(HttpServletRequest request, HttpServletResponse response,
                                          Integer subclinicId, String startDate, String endDate){

        SysUserInfo userInfo = (SysUserInfo) request.getSession().getAttribute(HttpUtils.CURRENT_USER_4_POJO);

        // 调用Service
        Map<String, Object> params = new HashMap<String, Object>() {{
//            if(userInfo.getSubclinicId() == 3 && userInfo.getUserRole() == 2){
                put("subclinicId",subclinicId);
//            }else{
//                put("subclinicId",userInfo.getSubclinicId());
//            }
//            put("subclinicId",userInfo.getSubclinicId());
            put("startDate", startDate);
            put("endDate", endDate);
        }};
        uploadFileService.exportBookingList(request, response, params);
        // 返回数据
        return null;
    }
    /**
     * @Title: exportPatientList
     * @Description: 导出患者列表
     * @param @return
     * @return ResponseData
     * @throws
     */
    @ApiOperation(value = "导出患者列表",notes="导出患者列表")
    @RequestMapping(value = "/exportPatientList",method = RequestMethod.GET)
    @ResponseBody
    public ResponseData exportPatientList(HttpServletRequest request, HttpServletResponse response,
                                          Integer subclinicId){
        SysUserInfo userInfo = (SysUserInfo) request.getSession().getAttribute(HttpUtils.CURRENT_USER_4_POJO);
        // 调用Service
        Map<String, Object> params = new HashMap<String, Object>() {{
            //if(userInfo.getSubclinicId() == 3 && userInfo.getUserRole() == 2){
                put("subclinicId",subclinicId);
//            }else{
//                put("subclinicId",userInfo.getSubclinicId());
//            }
        }};
        uploadFileService.exportPatientList(request, response, params);
        // 返回数据
        return null;
    }

    /**
     * @Title: exportTreatmentList
     * @Description: 导出病例列表
     * @param @return
     * @return ResponseData
     * @throws
     */
    @ApiOperation(value = "导出病例列表",notes="导出病例列表")
    @RequestMapping(value = "/exportTreatmentList",method = RequestMethod.GET)
    @ResponseBody
    public ResponseData exportTreatmentList(HttpServletRequest request, HttpServletResponse response,
                                          Integer subclinicId){
        SysUserInfo userInfo = (SysUserInfo) request.getSession().getAttribute(HttpUtils.CURRENT_USER_4_POJO);
        // 调用Service
        Map<String, Object> params = new HashMap<String, Object>() {{
//            if(userInfo.getSubclinicId() == 3 && userInfo.getUserRole() == 2){
                put("subclinicId",subclinicId);
//            }else{
//                put("subclinicId",userInfo.getSubclinicId());
//            }
        }};
        uploadFileService.exportTreatmentList(request, response, params);
        // 返回数据
        return null;
    }
    /**
     * @Title: importUserExcel
     * @Description: 导入会员信息
     * @param @return
     * @return ResponseData
     * @throws
     */
    @ApiOperation(value = "导入会员信息",notes="导入会员信息")
    @RequestMapping(value = "/importUserExcel",method = RequestMethod.POST)
    @ResponseBody
    public ResponseData importUserExcel(MultipartFile excelFile, HttpServletRequest request,
                                        HttpServletResponse response){
        ResponseData data = new ResponseData();

        SysUserInfo userInfo = (SysUserInfo) request.getSession().getAttribute(HttpUtils.CURRENT_USER_4_POJO);
        Map<String, Object> params = new HashMap<String, Object>() {{

        }};
        uploadFileService.importUserExcel(excelFile, request, response, params);
        // 返回数据

        return data;
    }

    /**
     * @Title: importPatientExcel
     * @Description: 导入患者信息
     * @param @return
     * @return ResponseData
     * @throws
     */
    @ApiOperation(value = "导入患者信息",notes="导入患者信息")
    @RequestMapping(value = "/importPatientExcel",method = RequestMethod.POST)
    @ResponseBody
    public ResponseData importPatientExcel(MultipartFile excelFile, HttpServletRequest request,
                                           HttpServletResponse response){
        ResponseData data = new ResponseData();

        Map<String, Object> params = new HashMap<String, Object>() {{

        }};
        uploadFileService.importUserExcel(excelFile, request, response, params);
        // 返回数据

        return data;
    }


    /**
     * 查询商品消耗列表下（分类）
     *
     * @param subclinicId
     * @param ifMonth
     * @param orderTime
     * @return
     */
    @ApiOperation(value = "查询商品消耗列表", notes = "查询商品消耗列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "subclinicId", dataType = "int", required = true, value = "门店id(全部传0,)", defaultValue = "0"),
            @ApiImplicitParam(paramType = "query", name = "ifMonth", dataType = "int", required = true, value = "月/日汇总（0，日 1，月）", defaultValue = "0"),
            @ApiImplicitParam(paramType = "query", name = "orderTime", dataType = "String", required = true, value = "汇总时间", defaultValue = "1"),
    })
    @RequestMapping(value = "/exportOrderList", method = RequestMethod.GET)
    @ResponseBody
    public ResponseData exportOrderList(HttpServletRequest request, HttpServletResponse response, int subclinicId, int ifMonth, String orderTime) {

        List<Map<String,Object>> goodsDetails = commonService.selectGoodsList(subclinicId, ifMonth, orderTime);
        List<Map<String,Object>> goodsLists = commonService.selectGoodsListDetail(subclinicId, ifMonth, orderTime);

        Map<String, Object> map = new HashMap<>();
        Integer orderNum = 0;
        if( goodsLists.size() > 0 ){
            for (Map<String,Object> goodsMap : goodsDetails){
                orderNum += Integer.parseInt(goodsMap.get("goodsNumber").toString());
            }
        }
        map.put("orderTime", orderTime);
        map.put("ifMonth", ifMonth);
        map.put("total", orderNum);
        map.put("totalType", goodsLists.size());
        map.put("goodList", goodsDetails);

        uploadFileService.exportOrderList(request, response, map);
        // 返回数据
        return null;

    }


}
